﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 坦克大战正式.Properties;

namespace 坦克大战正式
{
    enum Tag
    {
        MyTank,
        EnemyTank
    }
    class Bullet : Movething
    {
        public Tag Tag { get; set; }
        public bool IsDestroy { get; set; }
        public Bullet(int x, int y, int speed,Direction dir,Tag tag)
        {
            IsDestroy = false;
            this.X = x-Resources.BulletDown.Width/2+1;
            this.Y = y-Resources.BulletDown.Height/2+1;
            this.Speed = speed;
            BitmapDown = Resources.BulletDown;
            BitmapLeft = Resources.BulletLeft;
            BitmapRight = Resources.BulletRight;
            BitmapUp = Resources.BulletUp;
            this.Dir = dir;
            this.Tag = tag;
            //this.X -= Width/2;
            //this.Y -= Height/2;
        }
        //public override void DrawSelf()
        //{
        //    base.DrawSelf();
        //}
        public override void Update()
        {
            MoveCheck();//移动检查
            Move();

            base.Update();
        }
        private void MoveCheck()
        {
            #region 检查有没有超出窗体边界
            if (Dir == Direction.Up)
            {
                if (Y +Height / 2 +3< 0)
                {
                    IsDestroy = true; return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y+Height/2 - 3 > 450)
                {
                    IsDestroy = true; return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X + Width/2 - 3< 0)
                {
                    IsDestroy = true; return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Width /2 + 3 > 450)
                {
                    IsDestroy = true; return;
                }
            }
            #endregion

            //检查有没有和其它元素发生碰撞
            Rectangle rect = GetRectangle();
            rect.X = X + Width / 2 - 3;
            rect.Y = Y + Height / 2 - 3;
            rect.Height = 3;
            rect.Width = 3;

            int xExolosion = this.X + Width / 2;
            int yExolosion = this.Y + Height / 2;
            NotMovething wall = null;
            if ((wall = GameObjectManager.IsCollideWall(rect)) != null)
            {
                IsDestroy = true;
                GameObjectManager.DestroyWall(wall);
                GameObjectManager.CreatExplosion(xExolosion, yExolosion);
                SoundManger.PlayBlast();
                return;
            }
            if (GameObjectManager.IsCollideSteel(rect) != null)
            {
                GameObjectManager.CreatExplosion(xExolosion, yExolosion);
                IsDestroy = true; return;
            }
            if (GameObjectManager.IsCollideBoss(rect))
            {
                SoundManger.PlayBlast();

                GameFramework.ChangeToGameOver(); return;
            }
            if (Tag == Tag.MyTank)
            {
                EnemyTank tank = null;
                if((tank = GameObjectManager.IsCollideEnemyTank(rect))!=null)
                {
                    IsDestroy = true;
                    GameObjectManager.DestroyTank(tank);
                    GameObjectManager.CreatExplosion(xExolosion, yExolosion);
                    SoundManger.PlayHit();
                    return;
                }
            }
            else if(Tag == Tag.EnemyTank)
            {
                MyTank mytank = null;
                if ((mytank = GameObjectManager.IsCollideMyTank(rect)) != null)
                {
                    SoundManger.PlayBlast();
                    IsDestroy = true;
                    GameObjectManager.CreatExplosion(xExolosion, yExolosion);

                    mytank.TakeDamage();

                    return;
                }
            }
        }


        public void Move()
        {
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
            }
        }
    }
}
